package top.guoyang.springboot3.kaoshi.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.guoyang.springboot3.aop.AuthInfo;
import top.guoyang.springboot3.base.R;
import top.guoyang.springboot3.interceptor.AppInterceptor;
import top.guoyang.springboot3.kaoshi.entity.TbExamInfo;
import top.guoyang.springboot3.kaoshi.mapper.TbExamInfoMapper;
import top.guoyang.springboot3.kaoshi.service.TbExamInfoService;
import top.guoyang.springboot3.service.auth.AuthService;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbExamInfoServiceImpl implements TbExamInfoService {
    private final TbExamInfoMapper tbExamInfoMapper;

    private final AuthService authService;

    @Override
    public R<String> insert(TbExamInfo tbExamInfo) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();

        int i = tbExamInfoMapper.insert(tbExamInfo);
        return i == 1 ? R.ok("添加成功", "") : R.fail("添加失败", "");
    }


    @Override
    public R<String> update(TbExamInfo tbExamInfo) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();
        TbExamInfo check = tbExamInfoMapper.selectById(tbExamInfo.getId());
        if (check == null) {
            return R.fail("数据不存在", "");
        }
        if (!check.getId().equals(authInfo.getUid())) {
            return R.fail("只能更新自己的数据", "");
        }
        int i = tbExamInfoMapper.updateById(tbExamInfo);
        return i == 1 ? R.ok("更新成功", "") : R.fail("更新失败", "");
    }

    @Override
    public R<String> delete(Integer id) {
        AuthInfo authInfo = AppInterceptor.getAuthInfo();

        TbExamInfo check = tbExamInfoMapper.selectById(id);
        if (check == null) {
            return R.fail("数据不存在", "");
        }
        if (!check.getId().equals(authInfo.getUid())) {
            return R.fail("只能删除自己的数据", "");
        }
        int i = tbExamInfoMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", "") : R.fail("删除失败", "");
    }
}
